package com.es.www.kyl.bean;

import lombok.Data;

import com.es.www.kyl.constant.BusinessResultEnum;

/**
 * @Author by michangtao
 * @Date 2023/3/16 13:46
 * @Description
 */
@Data
public class R<T> {

    private String code;
    private T resData;
    private String msg;

    public R() {
    }

    public R(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> R<T> failCommonResponse() {
        return new R<T>(ResponseCodeEnum.FAIL.getResponseCode(), ResponseCodeEnum.FAIL.getResponseMessage());
    }

    public static <T> R<T> failResponse(String code, String  message) {
        return new R<>(code, message);
    }

    public static <T> R<T> failResponse(BusinessResultEnum businessResultEnum) {
        return new R<T>(businessResultEnum.getCode(), businessResultEnum.getMessage());
    }

    public static <T> R<T> successCommonResponse() {
        return new R<T>(ResponseCodeEnum.SUCCESS.getResponseCode(), ResponseCodeEnum.SUCCESS.getResponseMessage());
    }

    public static <T> R<T> successResponse(T resData) {
        R<T> response = successCommonResponse();
        response.setResData(resData);
        return response;
    }

    enum ResponseCodeEnum {
        SUCCESS("200", "Success"),
        FAIL("500", "Fail");

        private String responseCode;
        private String responseMessage;

        ResponseCodeEnum(String responseCode, String responseMessage) {
            this.responseCode = responseCode;
            this.responseMessage = responseMessage;
        }

        public String getResponseCode() {
            return this.responseCode;
        }

        public String getResponseMessage() {
            return this.responseMessage;
        }
    }
}
